<template>
    <div class="courseitem">
      <div class="head-wrap">
        <div class="head-video">
          <div class="vedios">
            <img :src="img()" style="width: 100%;height: 100%;cursor: pointer;">
          </div>
          <div class="item">
            <p class="name">{{detail.title}}</p>
            <p class="data">
              {{detail.popularity}}人在学&nbsp;&nbsp;&nbsp;&nbsp;课程总时长:{{detail.course_hour}}课时/
              {{detail.hour}}小时&nbsp;&nbsp;&nbsp;&nbsp;难度:{{detail.course_level}}
            </p>
            <div class="preferential">
              <p>{{detail.price_type}}</p>
              <p>距离结束:仅剩&nbsp;{{day}}天&nbsp;{{hour}}小时&nbsp;{{minute}}分<span>&nbsp;{{second}}</span>&nbsp;秒</p>

            </div>
            <p class="price">
              <span>活动价</span>
              <span class="sp1">￥{{detail.now_price}}</span>
              <span class="sp2">￥{{detail.cost_price}}</span>
            </p>
            <div class="bottom">
              <p class="btns">
                <button class="btn1">立即购买</button>
                <button class="btn2">免费试学</button>
              </p>
              <p class="add" @click="addShopping_car_num()">
                <img src=""/>
                加入购物车
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
</template>

<script>
  export default {
    name: "courseDetail",
    data(){
      return{
        detail:{
          course_hour:null,
          course_img:null,
          course_level:null,
          hour:null,
          popularity:null,
          promotion_end_date:null,
          title:null,
          price_type:null,
          now_price:null,
          cost_price:null
        },
        day:null,
        hour:null,
        minute:null,
        second:null,
      }
    },
    mounted(){
      this.getMsg();
      this.countTime();
    },
    methods:{
      img(){
        return require('../assets/'+this.detail.course_img)
      },
      addShopping_car_num(){
        this.$store.commit("save_shopping_car_number",{title:this.detail.title})
        console.log(this.$store.state.shopping_car)
      },

      getMsg(){
        var _this = this;
        var id = this.$route.params.id;

        this.$axios.request({
          url:'http://127.0.0.1:8010/api/v1/courses/'+id+'/details/',
          method:'get',
          headers:{
            'Content-Type':'application/json',
          }
        }).then(function (msg) {
          if(msg.data.code === 1){
            _this.detail = msg.data.data
          }else{
            console.log(msg.data)
          }
        }).catch(function (msg) {
          console.log(msg.data)
        })
      },
      countTime(){
        // 获取当前时间
        var date = new Date();
        var now = date.getTime();
        // 设置截止时间
        var endDate = new Date(this.detail.promotion_end_date);
        var end = endDate.getTime();
        // 时间差
        var leftTime = end - now;

        if(leftTime >= 0 ){
          this.day = Math.floor(leftTime /1000 /60 /60 /24);
          this.hour = Math.floor(leftTime / 1000 / 60 / 60 % 24);
          this.minute = Math.floor(leftTime / 1000 / 60 % 60 );
          this.second = Math.floor(leftTime / 1000 % 60);
        }setTimeout(this.countTime, 1000);
      },
    }

  }
</script>

<style scoped>
  .courseitem{
    width: 100%;
    height: auto;
    background: #fafafa;
  }
  .head-wrap{
    width: 100%;
    height: auto;
    background: #fff;
    padding-top: 30px;
  }
  .head-video{
    width: 1200px;
    height: auto;
    display: flex;
    margin: 0 auto;
  }
  .vedios{
    width: 690px;
    height: 388px;
    background: #000;
    overflow: hidden;
  }
  .item{
    background: #fff;
    position: relative;
  }
  .name{
    font-size: 20px;
    color: #333;
    padding: 10px 23px;
    letter-spacing: .45px;
  }
  .data{
    padding-left: 23px;
    padding-right: 23px;
    padding-bottom: 16px;
    font-size: 14px;
    color: #9b9b9b;
  }
  .preferential{
    width: 100%;
    height: auto;
    background: #fa6240;
    font-size: 14px;
    color: #4a4a4a;
    display: -ms-flexbox;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 23px;
  }
  .preferential p:first-of-type{
    font-size: 16px;
    color: #fff;
    letter-spacing: .36px;
  }
  .preferential>p:nth-of-type(2){
    font-size: 14px;
    color: #fff;
  }
  .preferential>p:nth-of-type(2)>span{
    width: 24px;
    height: auto;
    display: inline-block;
    background: #fafafa;
    color: #5e5e5e;
    padding: 6px 0;
    text-align: center;
  }
  .price{
    width: 100%;
    height: auto;
    font-size: 14px;
    color: #4a4a4a;
    display: flex;
    align-items: flex-end;
    padding: 5px 23px;
    background: #fff;
  }
  .sp1{
    font-size: 26px;
    color: #fa6240;
    margin-left: 10px;
    display: inline-block;
    margin-bottom: -5px;
  }
  .sp2{
    font-size: 14px;
    color: #9b9b9b;
    margin-left: 10px;
    text-decoration: line-through;
  }
  .bottom{
    display: flex;
    align-items: center;
    position: absolute;
    left: 0;
    bottom: 20px;
    width: 100%;
    height: auto;
    justify-content: space-between;
    padding-left: 23px;
    padding-right: 23px;
  }
  .btns{
    display: flex;
    align-items: center;
  }
  .btn1{
    width: 125px;
    height: 40px;
    background: #ffc210;
    border-radius: 4px;
    color: #fff;
    cursor: pointer;
    margin-right: 15px;
  }
  .btn2{
    background: #fff;
    color: #ffc210;
    border: 1px solid #ffc210;
    width: 125px;
    height: 40px;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 15px;
  }
  .add{
    font-size: 14px;
    color: #ffc210;
    text-align: center;
    display: flex;
    align-items: center;
    cursor: pointer;
  }
  .add>img{
    width: 20px;
    height: auto;
    margin-right: 7px;
  }
</style>
